package dailyTopic;

/**
 * @author zhangmin
 * @create 2021-08-21 12:50
 */
public class compress443 {
    public int compress(char[] chars) {
        int n=chars.length;
        if (n==1)
            return 1;
        StringBuffer s=new StringBuffer();
        char curChar=chars[0];
        int cnt=1;
        for (int i = 1; i < n; i++) {
            System.out.println("chars"+i+"="+chars[i]);
            if (chars[i]!=curChar){
                s.append(curChar);
                System.out.println("s="+s);
                if (cnt>1){
                    s.append(cnt);
                    System.out.println("s="+s);
                }
                curChar=chars[i];
                cnt=1;
            }else {
                cnt++;
                System.out.println("cnt=" + cnt);
            }
        }
        s.append(curChar);
        if (cnt>1){
            s.append(cnt);
        }

        String res=s.toString();
        for (int i = 0; i < res.length(); i++) {
            chars[i]=res.charAt(i);
        }
        return res.length();
    }

    public static void main(String[] args) {
        char[] chars =new char[]{'a','a','b','b','c','c','c'};
        compress443 object=new compress443();
        System.out.println(object.compress(chars));
    }
}
